package com.qbt.unipock.agent.services;

import java.util.UUID;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//import com.qbt.unipock.agent.repository.DistributorPassportRepository;
import com.qbt.unipock.agent.repository.DistributorRepository;
//import com.qbt.unipock.agent.repository.DistributorPassportRepository;
//import com.qbt.unipock.agent.repository.DistributorPreferenceRepository;
//import com.qbt.unipock.agent.repository.DistributorRepository;
import com.xjoyt.pay.abstracts.AbstractService;
import com.xjoyt.pay.annotations.LockMethod;
import com.xjoyt.pay.annotations.LockParam;
import com.xjoyt.pay.commons.ObjectUtils;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.master.po.CompanyPreference;
import com.xjoyt.pay.master.po.DistributorPassport;
import com.xjoyt.pay.merchant.register.params.DistributorPageParam;
import com.xjoyt.pay.master.po.Distributor;
import com.xjoyt.pay.params.ChangePasswordParam;


@Service
public class DistributorService extends AbstractService {

	@Autowired
	private DistributorRepository repository;
	
//	@Autowired
//	private DistributorPassportRepository distributorPassportRepository;
	
//	@Autowired
//	private DistributorPreferenceRepository distributorPreferenceRepository;

//	@Autowired
	//private PassportSecurity passportSecurity;

//	@Autowired
//	private CompanyPreferenceRepository companyPreferenceRepository;

	//@Autowired
//	private CompanyRepository companyRepository;

	public Distributor get(String id) {
		return repository.findOne(id);
		
	}

	public PageResult<Distributor> loadPage(PageParam<DistributorPageParam> param) {
		return repository.loadPage(param);
	}
/*
	public ExecuteResult<Distributor> getForLogin(String account, String password) {
		Distributor entity = repository.findOneByAccount(account);
		if (entity == null)
			return ExecuteResult.fail("代理商不存在");
		if (!BooleanUtils.isTrue(entity.enabled))
			return ExecuteResult.fail("代理商已被停用");
		if (entity.company.enabled == null || !entity.company.enabled)
			return ExecuteResult.fail("公司已被停用");
		DistributorPassport passport = distributorPassportRepository.findOneByDistributorIdAndEnabled(entity.id);
		if (passport == null)
			return ExecuteResult.fail("密码错误，请找回密码");
		if (!passportSecurity.isSuccess(password, new PassportHex(passport.password, passport.key, passport.type))) {
			return ExecuteResult.fail("密码错误，请查证后重新输入");
		}
		return ExecuteResult.ok(entity);
	}*/

	public ExecuteResult<?> changePassword(ChangePasswordParam param) {
		if (StringUtils.isBlank(param.oldPassword) || StringUtils.isBlank(param.newPassword)
				|| StringUtils.isBlank(param.password)) {
			return ExecuteResult.fail("密码不能为空");
		}

		if (!param.password.equals(param.newPassword)) {
			return ExecuteResult.fail("两次密码不一致");
		
		}

		Distributor entity = repository.findOne(param.id);
		if (entity == null)
			return ExecuteResult.fail("代理商不存在");
/*
		DistributorPassport passport = distributorPassportRepository.findOneByDistributorIdAndEnabled(entity.id);
		if (passport == null) {
			if ("123456".equals(param.oldPassword)) {
				return ExecuteResult.ok(entity);
			}
			return ExecuteResult.fail("密码错误");
		} else if (!passportSecurity.isSuccess(param.oldPassword,
				new PassportHex(passport.password, passport.key, passport.type))) {
			return ExecuteResult.fail("密码错误，请查证后重新输入");
		}

		distributorPassportRepository.save(entity, param.password);*/
		return ExecuteResult.ok();
	}

	@LockMethod(block = true)
	public ExecuteResult<Distributor> create(@LockParam("id") Distributor param) {
		//Distributor entity = repository.findOne(param.id);
		/*
		if (entity == null) {
			entity = new Distributor();
		}
		ObjectUtils.fieldCopy(param, entity);
		if (StringUtils.isBlank(entity.no)) {
			CompanyPreference preference = companyPreferenceRepository.findOne(param.companyId);
			entity.no = preference == null || StringUtils.isBlank(preference.distributorPrefix) ? StringUtils.EMPTY
					: preference.distributorPrefix.toUpperCase();
			entity.no += createMerchantNo();
		}
		entity.enabled = true;
		if (StringUtils.isBlank(entity.key)) {
			entity.key = UUID.randomUUID().toString().replaceAll("-", StringUtils.EMPTY);
		}
		if (StringUtils.isNotBlank(param.password)) {
			distributorPassportRepository.save(entity, param.password);
		}
		if (StringUtils.isNotBlank(param.companyId)) {
			entity.company = companyRepository.findOne(param.companyId);
		}*/
		repository.save(param);
	//	distributorPreferenceRepository.save(entity);

		return ExecuteResult.ok(param);
	}

	public ExecuteResult<String> resetKey(String id) {
		Distributor entity = repository.findOne(id);
		if (entity == null) {
			return ExecuteResult.fail("数据不存在");
		}
		entity.key = UUID.randomUUID().toString().replaceAll("-", StringUtils.EMPTY);
		repository.save(entity);
		return ExecuteResult.ok(entity.key);
	}

	public Distributor getByNo(String no) {
		return repository.findOneByNo(no);
	}

	private String createMerchantNo() {
		Long count = repository.getLastNo();
		return StringUtils.leftPad(String.valueOf(++count), 15, "0");
	}

	public ExecuteResult<Distributor> update(Distributor param) {
		Distributor entity = repository.findOne(param.id);
		if (entity == null) {
			return ExecuteResult.fail("代理商不存在");
		}
		Distributor distributor = repository.save(param);
		return ExecuteResult.ok(distributor);
	}

	

	public ExecuteResult<Distributor> getForLogin(String account, String password) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
